<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<style type="text/css">
			td{
				text-align: center;
			}
		</style>
	</head>

	<body>
		<script type="text/javascript">
			var nian = 2012;
			var yue = 2;
			var day = 0;
			var sumday = 0;
			var isrun = false;
			if(nian % 4 == 0 && nian % 100 != 0 || nian % 400 == 0) {
				isrun = true;
			}
			if(yue == 1 || yue == 3 || yue == 5 || yue == 7 || yue == 8 || yue == 10 || yue == 12) {
				day = 31;
			} else if(yue == 4 || yue == 6 || yue == 9 || yue == 11) {
				day = 30;
			} else if(yue == 2) {
				if(isrun) {
					day = 29;
				} else {
					day = 28;
				}
			}
			//求年份天数差
			for(var n = 1900; n < nian; n++) {
				if(n % 4 == 0 && n % 100 != 0 || n % 400 == 0) {
					sumday += 366;
				} else {
					sumday += 365;
				}
			}

			//月份天数差
			for(var y = 1; y < yue; y++) {
				switch(y) {
					case 1:
					case 3:
					case 5:
					case 7:
					case 8:
					case 10:
					case 12:
						sumday += 31;
						break;
					case 4:
					case 6:
					case 9:
					case 11:
						sumday += 30;
						break;
					case 2:
						//						if (isrun) {
						//							sumday+=29;
						//						}else{
						//							sumday+=28;
						//						}
						//条件运算符，三元运算符
						//  条件     ？ 真    ： 假 	
						sumday += isrun ? 29 : 28;
						break;
				}

			}

			var day1week = (sumday + 1) % 7;
			document.write(day1week);

			document.write("<table border='1px' cellpadding='0'cellspacing='0'width='400px'>");
			document.write("<tr><th colspan='7'>" + nian + "年" + yue + "月共" + day + "天<br />" + "</th></tr>")
			document.write("<tr><th>日</th><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th></tr>")
			document.write("<tr>")
			//打印空的td
			for(var i = 0; i < day1week; i++) {
				document.write("<td></td>");
			}
			for(var i = 1; i <= day; i++) {
				if((day1week + i - 1) % 7 == 0 && i != 1) {
					document.write("<tr>");
				}
				document.write("<td>" + i + "</td>");
if((day1week + i - 1) % 7 == 6) {
	document.write("</tr>");
}

}

document.write("</table>");
		</script>
	</body>

</html>